package com.itheima.servlet.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

// req02?username=zhangsan&password=123
// 演示Request获取请求数据
@WebServlet("/req02")
public class RequestDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // String getMethod()：获取请求方式： GET
        String method = req.getMethod();
        System.out.println("method = " + method);

        // String getContextPath()：获取虚拟目录(项目访问路径)：/request-response-demo
        String contextPath = req.getContextPath();
        System.out.println("contextPath = " + contextPath);

        // StringBuffer getRequestURL(): 获取URL(统一资源定位符)：http://localhost:8080/request-response-demo/req02
        String s = req.getRequestURL().toString();
        System.out.println("URL = " + s);

        // String getRequestURI()：获取URI(统一资源标识符)： /request-response-demo/req02
        String requestURI = req.getRequestURI();
        System.out.println("requestURI = " + requestURI);

        // String getQueryString()：获取请求参数（GET方式）： username=zhangsan&password=123
        String queryString = req.getQueryString();
        System.out.println("queryString = " + queryString);

        //------------
        // String getHeader(String name)：根据请求头名称，获取值
        // 获取请求头：user-agent: 浏览器的版本信息
        String header = req.getHeader("User-Agent");
        System.out.println("header = " + header);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取post 请求体：请求参数
        // 1.获取字符输入流
        BufferedReader reader = req.getReader();
        // 2.读取数据
        String line = reader.readLine();
        System.out.println("line = " + line);
    }
}
